package cn.knight.controller;

import cn.knight.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URL;

@RestController
public class UserCtrl {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getByIdPrepare")
    @GetMapping("/getById/{id}")
    public User getById(@PathVariable long id){

        String url = "http://user-server/user/"+id;

        return restTemplate.getForObject(url, User.class);
    }

    public User getByIdPrepare(@PathVariable Long id){
        return User.builder().id(-1L).desc("系统繁忙，请稍后再试").build();
    }
}
